﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project.Workflow;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.UI.Properties;

namespace SoftwareConsulting.BI11.Studio.UI.Workflow
{
    public partial class NewWorkflowRecordDialog : RestorableForm
    {
        private SoftwareConsulting.BI11.Studio.Core.Project.Workflow.IProjectWorkflow _workflow; //редактируемая система документооборота
        private IWorkflowRecord _parentRecord; //родительская запись

        public NewWorkflowRecordDialog(SoftwareConsulting.BI11.Studio.Core.Project.Workflow.IProjectWorkflow workflow, IWorkflowRecord parentRecord)
        {            
            _workflow = workflow;
            _parentRecord = parentRecord;
            InitializeComponent();
        }

        /// <summary>
        /// Убирает надпись "Введите заголовок"
        /// </summary>
        private void FreeCaption()
        {
            if (CaptionBox.Text == Resources.BI_WORKFLOW_NEWRECORD_CAPTION)
            {
                CaptionBox.Text = "";
                CaptionBox.ForeColor = SystemColors.WindowText;
            }
        }

        /// <summary>
        /// Проверяет введен ли заголовок
        /// </summary>
        private void CheckCaption()
        {
            CaptionBox.Text = CaptionBox.Text.Trim();
            if (string.IsNullOrEmpty(CaptionBox.Text))
            {
                CaptionBox.Text = Resources.BI_WORKFLOW_NEWRECORD_CAPTION;
                CaptionBox.ForeColor = SystemColors.GrayText;
            }
        }

        /// <summary>
        /// Добавляет новую запись и закрывает диалог
        /// </summary>
        private void ApplyAndClose()
        {
            CheckCaption();
            string newcaption = CaptionBox.Text;
            newcaption = newcaption == Resources.BI_WORKFLOW_NEWRECORD_CAPTION ? "" : newcaption;
            string newtext = MessageBox.Text.Trim();
            if (string.IsNullOrEmpty(newtext))
            {
                GeneralMethods.ShowUserError(Resources.BI_WORKFLOW_MESSAGE_CANTBEEMPTY);
                MessageBox.Focus();
                MessageBox.SelectAll();
                DialogResult = DialogResult.None;
                return;
            }
            _workflow.Add(_parentRecord, newcaption, newtext, null, WorkflowAttachmentKind.CalculationResults);
            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Инициализирует диалог
        /// </summary>
        private void Initialize()
        {
            CheckCaption();
        }

        private void CaptionBox_Leave(object sender, EventArgs e)
        {
            CheckCaption();
        }

        private void CaptionBox_Enter(object sender, EventArgs e)
        {
            FreeCaption();
        }

        private void NewWorkflowRecordDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }

        private void CaptionBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Focus();
            }
        }
    }
}
